package net.ttny.java.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.baidu.aip.util.Util;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;

public class AipImageClassify extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	public JSONObject iMessage;
	
	public static final String APP_ID = "";
	public static final String API_KEY = "";
	public static final String SECRET_KEY = "";

    public JSONObject getiMessage() {
		return iMessage;
	}

	public void setiMessage(JSONObject res) {
		this.iMessage = res;
	}

	public String execute() throws IOException {
		com.baidu.aip.imageclassify.AipImageClassify client = new com.baidu.aip.imageclassify.AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
		
		HashMap<String, String> options = new HashMap<String, String>();
		
		client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        
        String image = "D:/niyan/wamp/www/test/pic/2018_04_041.png";
        byte[] imgData = Util.readFileByBytes(image);
        JSONObject res = ((com.baidu.aip.imageclassify.AipImageClassify) client).advancedGeneral(imgData, options);
        System.out.println(res.toString(2));
        this.setiMessage(res);
		
		return SUCCESS;
	}
}
